﻿<?php
require_once('../db/check_login.php');
require_once('../db/config.php');
$db=new db($config);
$id = $_GET['id'];
$roleName = $db->foundbyId('role', $id); //查找角色名称																																							
$role_columns = $db->foundbyWhere('role_columns', "r_id=$id");
//var_dump($role_columns);
//根据角色id查找的到的角色栏目对应的信息																				
$columnDatas = $db->foundbyWhere('columns'); //加上s避免与系统冲突
$columnCounts = $db->countbyWhere('columns');
?>
<body style="font-size:18px;">
<form id="form1" name="form1" method="post" action="">
<?php if(!empty($columnDatas)):?>
<?php foreach($columnDatas as $val):?>
<?php echo $val['name']?>:<input type="checkbox" class='columns' name="columns[]" attrId="<?php echo $val['id']?>"/>
<br/>
<?php if($val['power']&1):?>
查看:<input type="checkbox" class='columns_action_<?php echo $val['id']?>' name="columns_action[]"  value="<?php echo $val['id']?>_1" <?php foreach($role_columns as $value):?> 
<?php if($value['c_id']==$val['id']&&$value['power']==1):?>
checked="checked"
<?php endif;?>
<?php endforeach; ?>/>
<?php endif;?>
<?php if($val['power']&2):?>
增加:<input type="checkbox" class='columns_action_<?php echo $val['id']?>' name="columns_action[]" value="<?php echo $val['id']?>_2" <?php foreach($role_columns as $value):?> 
<?php if($value['c_id']==$val['id']&&$value['power']==2):?>
checked="checked"
<?php endif;?>
<?php endforeach; ?>/>
<?php endif;?>
<?php if($val['power']&4):?>
修改:<input type="checkbox" class='columns_action_<?php echo $val['id']?>' name="columns_action[]" value="<?php echo $val['id']?>_4" <?php foreach($role_columns as $value):?> 
<?php if($value['c_id']==$val['id']&&$value['power']==4):?>
checked="checked"
<?php endif;?>
<?php endforeach; ?>/> 
<?php endif;?>
<?php if($val['power']&8):?>
删除:<input type="checkbox" class='columns_action_<?php echo $val['id']?>' name="columns_action[]" value="<?php echo $val['id']?>_8" <?php foreach($role_columns as $value):?> 
<?php if($value['c_id']==$val['id']&&$value['power']==8):?>
checked="checked"
<?php endif;?>
<?php endforeach; ?>/>
<?php endif;?>
</br>
<hr/>
<?php endforeach;?>
<?php endif;?>
<input type="submit" name="sub" id="sub"  style=" width:80px; height:30px;" value="提交" />
</form>
</body>
<script type="text/javascript" src="../js/jquery-2.0.js"></script> 
<script language="javascript">
$(document).ready(function() {
    $('.columns').click(function() {
        var id = $(this).attr('attrId');
        if (this.checked) {
            $('.columns_action_' + id).each(function() {
                this.checked = true;
            });
        } else {
            $('.columns_action_' + id).each(function() {
                this.checked = false;
            });
        }
    });
});
</script>
<?php
if (isset($_POST['sub'])) {
  $row=$db->countbyWhere('role_columns',"r_id=$id");
  if($row>0){
  $db->delete('role_columns', "r_id=$id");
  }
  $columns_action=$_POST['columns_action'];
  $id = $_GET['id'];
  foreach($columns_action as $val){
  $data=explode('_',$val);
  $insertData = array(
  'r_id' => $id,
  'c_id' => $data[0],
  'power' =>$data[1]
   );
  $db->insert('role_columns',$insertData);		
  }
  header('location:list.php');
}
?>
